package demo.dcs.aspect;

import demo.dcs.MertricListener;
import demo.dcs.MertricSendSchedule;
import demo.java.lang.management.vo.MetricSpan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;



/**
 * @author mandy.hu
 *
 */
public class ExecutorPoolAspectJ extends AbstractXdcsAspectJ implements MertricListener {
	
	private static final Logger logger = LoggerFactory.getLogger(ExecutorPoolAspectJ.class);
	
	private ThreadPoolExecutor threadPoolExecutor;
	
	private int corePoolSize;
    private int maximumPoolSize;
    private long keepAliveTime;
    private TimeUnit unit;
    private BlockingQueue<Runnable> workQueue;

    
    public ExecutorPoolAspectJ(){
    	MertricSendSchedule.getMertricSendSchedule().addMertricListener(this);
    }
    
    @Override
	public void beforeAspectJ(Object signature, Object instance, Object[] args) {

		
	}

	@Override
	public void afterAspectJ(Object result, Object signature, Object instance, Object[] args, Throwable throwable) {
		try {
			if (!mercurySwitch()) {
				return;
			}
			if (instance instanceof ThreadPoolExecutor) {   
				threadPoolExecutor = (ThreadPoolExecutor) instance; 
				if (args.length >=5) {
					for (Object object : args) {
						if (args[0] instanceof Integer) {
							corePoolSize = (Integer)args[0];
						}
						if (args[1] instanceof Integer) {
							maximumPoolSize = (Integer)args[1];
						}
						if (args[2] instanceof Long) {
							keepAliveTime = (Long)(args[2]);					
												}
						if (args[3] instanceof TimeUnit) {
							unit  = (TimeUnit)args[3];
						}
						if (args[4] instanceof BlockingQueue) {
							workQueue = (BlockingQueue)args[4];
						}
						
					}
				}
				
			}
		} catch (Throwable e) {
			logger.error("ExecutorPoolAspectJ beforeAspectJ", e);
		}
	}


	@Override
	public List<MetricSpan> collectMetric() {
		
		return null;
	}

    @Override
    public boolean isSame(MertricListener listener) {
        return false;
    }

}
